﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using WebApi.Core.IRepository.Base;
using WebApi.Core.IService.Base;
using WebApi.Core.Model.ResponseModels;

namespace WebApi.Core.Service.Base
{ 
    /// <summary>
    /// 服务基类
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public class BaseService<TEntity> : IBaseService<TEntity> where TEntity : class, new()
    {
        private readonly IBaseRepository<TEntity> _baseRepository;
        //这里使用依赖注入
        public BaseService(IBaseRepository<TEntity> baseRepository)
        {
            _baseRepository = baseRepository;  
        }

        public async Task<int> InsertAsync(TEntity model)
        {
            return await _baseRepository.InsertAsync(model);
        }

        public async Task<List<int>> InsertBatchAsync(List<TEntity> model)
        {
            return await _baseRepository.InsertBatchAsync(model);
        }


        public async Task<bool> UpdateAsync(TEntity entity)
        {
            return await _baseRepository.UpdateAsync(entity);
        }
        public async Task<bool> UpdateAsync(TEntity entity, Expression<Func<TEntity, bool>> where, bool ignoreNull = true)
        {
            return await _baseRepository.UpdateAsync(entity, where, ignoreNull);
        }
        public async Task<bool> UpdateAsync(TEntity entity, Expression<Func<TEntity, Object>>? columns = null, Expression<Func<TEntity, bool>>? where = null, bool ignoreNull = true)
        {
            return await _baseRepository.UpdateAsync(entity,columns, where, ignoreNull);
        }


        public async Task<bool> DeleteByIdAsync(object id)
        {
            return await _baseRepository.DeleteByIdAsync(id);
        }

        public async Task<bool> DeleteByIdsAsync(object[] ids)
        {
            return await _baseRepository.DeleteByIdsAsync(ids);
        }


        public async Task<TEntity> QueryById(object id)
        {
            return await _baseRepository.QueryById(id);
        }
        public async Task<TEntity> QueryAsync(Expression<Func<TEntity, bool>> where)
        {
            return await _baseRepository.QueryAsync(where);
        }

        public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true)
        {
            return await _baseRepository.QueryAsync(whereExpression, orderByExpression, isAsc);
        }

        public async Task<PageResult<TEntity>> QueryPageAsync(Expression<Func<TEntity, bool>> whereExpression, int pageIndex, int pageSize, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true)
        {  
            return await _baseRepository.QueryPageAsync(whereExpression, pageIndex, pageSize, orderByExpression, isAsc);
        }


        public async Task<bool> ExistAsync(Expression<Func<TEntity, bool>> whereExpression)
        {
            return await _baseRepository.ExistAsync(whereExpression);
        }

        public async Task<int> CountAsync(Expression<Func<TEntity, bool>> whereExpression)
        {
            return await _baseRepository.CountAsync(whereExpression);
        }

    }
}
